/*
 * $Id: NavigationContextMenu.java 134 2009-07-13 10:02:12Z donbeave $
 */

package labour.manager.client.widget.menu;

import labour.concept.client.data.Locale;
import labour.concept.client.data.ObjectText;
import labour.concept.engine.client.data.Site;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;

public class NavigationContextMenu extends Menu {

  private TreePanel<ModelData> tree;

  private SiteNavigationMenu siteMenu;
  private ObjectNavigationMenu objectMenu;
  private LocaleNavigationMenu localeMenu;

  public NavigationContextMenu(TreePanel<ModelData> tree) {
    this.tree = tree;
  }

  @Override
  public void showAt(int x, int y) {
    if (tree.getSelectionModel().getSelectedItem() instanceof Site) {
      getSiteMenu().setItem((Site) tree.getSelectionModel().getSelectedItem());
      getSiteMenu().showAt(x, y);
    }
    else if (tree.getSelectionModel().getSelectedItem()  instanceof Locale) {
      Locale locale = (Locale) tree.getSelectionModel().getSelectedItem();

      if (locale.getManager().getDefaultLocale().equals(locale)) {
        getLocaleMenu().getUseByDefaultItem().disable();
      } else {
        getLocaleMenu().getUseByDefaultItem().enable();
      }

      getLocaleMenu().showAt(x, y);
    }
    else if (tree.getSelectionModel().getSelectedItem()  instanceof ObjectText) {
      getObjectMenu().setItem((ObjectText) tree.getSelectionModel().getSelectedItem());
      getObjectMenu().showAt(x, y);
    }
  }

  public SiteNavigationMenu getSiteMenu() {
    if (siteMenu == null) {
      siteMenu = new SiteNavigationMenu();
    }
    return siteMenu;
  }

  public LocaleNavigationMenu getLocaleMenu() {
    if (localeMenu == null) {
      localeMenu = new LocaleNavigationMenu();
    }
    return localeMenu;
  }

  public ObjectNavigationMenu getObjectMenu() {
    if (objectMenu == null) {
      objectMenu = new ObjectNavigationMenu();
    }
    return objectMenu;
  }

  public void setObjectMenu(ObjectNavigationMenu menu) {
    objectMenu = menu;
  }

}
